#include<iostream>
using namespace std;
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};
int checksize(struct TreeNode *root)
{
    return root==NULL?0:checksize(root->left)+checksize(root->right)+1;
}
void preorder(struct TreeNode* root,int *a,int *pi)
{
    if(root==NULL)
    {
        (*pi)++;
        return;
    }
    a[(*pi)++]=root->val;
    preorder(root, a, pi);
    preorder(root, a, pi);
    return;
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
    *returnSize=checksize(root);
    int *a=(int *)malloc(sizeof(int)* *returnSize);
    preorder(root,a,0);
    return a;
}